package com.etheart.framework.core.fulltextsearch;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.etheart.framework.core.dao.Page;

@Repository("etheart_FullTextQueryService")
public class FullTextQueryServiceImpl implements FullTextQueryService {

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public FullTextQueryServiceImpl(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public <T> Page <T> find(final FullTextQueryParameter<T> param,
			final int pageNo, final int size) {
	
		SearchCallback<T> callback = new SearchCallback<T>(param);
		
		
		callback.setFirstResult((pageNo-1)*size);
		callback.setMaxResults(size); 
		Object result = hibernateTemplate.execute(callback);
		return (Page<T>)result;
	}



	public <T> long getRowCount(FullTextQueryParameter<T> param) {
		SearchResultSizeCallback<T> resultSizeCallback = new SearchResultSizeCallback<T>(
				param);
		long rowCnt = (Integer) hibernateTemplate.execute(resultSizeCallback);
		return rowCnt;
	}
}
